/*
 * Copyright (c) 2022 Yubico AB. All rights reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file.
 * SPDX-License-Identifier: BSD-2-Clause
 */

#undef NDEBUG

#include <assert.h>
#include <string.h>

#include <openssl/sha.h>

#define _FIDO_INTERNAL

#include <fido.h>

/*
 * zlib compressed data (RFC1950); see https://www.ietf.org/rfc/rfc6713.txt
 */
static /* const */ unsigned char rfc1950_blob[694] = {
	0x78, 0x9c, 0xb5, 0x52, 0x3b, 0x6f, 0xdb, 0x30,
	0x10, 0xde, 0xf5, 0x2b, 0x0e, 0x99, 0x12, 0x40,
	0x75, 0x13, 0x4f, 0x45, 0x3b, 0xd1, 0x12, 0x6d,
	0x1d, 0x20, 0x8b, 0x2a, 0x49, 0xd9, 0xf5, 0x28,
	0x4b, 0x4c, 0x42, 0xc0, 0x12, 0x03, 0x3d, 0x12,
	0xe4, 0xdf, 0xf7, 0xc8, 0x3a, 0x88, 0xd3, 0x0c,
	0x9d, 0xea, 0xc1, 0x3e, 0xf3, 0x8e, 0xdf, 0xeb,
	0x98, 0xb8, 0xa7, 0xd7, 0xc1, 0x3e, 0x3c, 0x4e,
	0x70, 0xdd, 0xdc, 0xc0, 0xf2, 0xf6, 0xee, 0xdb,
	0x97, 0xe5, 0xed, 0x72, 0x09, 0x87, 0xf9, 0x68,
	0x1b, 0x07, 0x6c, 0xb5, 0x00, 0x76, 0x3a, 0x41,
	0x18, 0x19, 0x61, 0x30, 0xa3, 0x19, 0x9e, 0x4d,
	0xbb, 0x88, 0x22, 0x69, 0x5a, 0x3b, 0x4e, 0x83,
	0x3d, 0xce, 0x93, 0x75, 0x3d, 0xd4, 0x7d, 0x0b,
	0xf3, 0x68, 0xc0, 0xf6, 0x30, 0xba, 0x79, 0x68,
	0x4c, 0x38, 0x39, 0xda, 0xbe, 0x1e, 0x5e, 0xe1,
	0xde, 0x0d, 0xdd, 0x18, 0xc3, 0x8b, 0x9d, 0x1e,
	0xc1, 0x0d, 0xe1,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0x2c, 0xc9, 0xce, 0x71, 0x2f,
	0xa2, 0xdf, 0xdf, 0xd7, 0x8e, 0x9c,
};

/*
 * expected sha256 of rfc1950_blob after decompression
 */
static const unsigned char rfc1950_blob_hash[SHA256_DIGEST_LENGTH] = {
	0x61, 0xc0, 0x4e, 0x14, 0x01, 0xb6, 0xc5, 0x2d,
	0xba, 0x15, 0xf6, 0x27, 0x4c, 0xa1, 0xcc, 0xfc,
	0x39, 0xed, 0xd7, 0x12, 0xb6, 0x02, 0x3d, 0xb6,
	0xd9, 0x85, 0xd0, 0x10, 0x9f, 0xe9, 0x3e, 0x75,

};

static const size_t rfc1950_blob_origsiz = 1322;

static /* const */ unsigned char random_words[515] = {
	0x61, 0x74, 0x68, 0x69, 0x72, 0x73, 0x74, 0x20,
	0x54, 0x68, 0x6f, 0x20, 0x63, 0x6f, 0x74, 0x20,
	0x73, 0x70, 0x6f, 0x66, 0x66, 0x79, 0x20, 0x4a,
	0x61, 0x76, 0x61, 0x6e, 0x20, 0x62, 0x72, 0x65,
	0x64, 0x65, 0x73, 0x20, 0x4c, 0x41, 0x4d, 0x20,
	0x6d, 0x69, 0x73, 0x2d, 0x68, 0x75, 0x6d, 0x69,
	0x6c, 0x69, 0x74, 0x79, 0x20, 0x73, 0x70, 0x69,
	0x67, 0x6f, 0x74, 0x20, 0x72, 0x65, 0x76, 0x6f,
	0x6c, 0x74, 0x69, 0x6e, 0x67, 0x6c, 0x79, 0x20,
	0x49, 0x6f, 0x64, 0x61, 0x6d, 0x6f, 0x65, 0x62,
	0x61, 0x20, 0x68, 0x79, 0x70, 0x6f, 0x68, 0x79,
	0x64, 0x72, 0x6f, 0x63, 0x68, 0x6c, 0x6f, 0x72,
	0x69, 0x61, 0x20, 0x76, 0x6f, 0x6c, 0x75, 0x6d,
	0x65, 0x74, 0x74, 0x65, 0x20, 0x61, 0x63, 0x72,
	0x69, 0x64, 0x69, 0x6e, 0x65, 0x20, 0x68, 0x6f,
	0x77, 0x6c, 0x20, 0x45, 0x75, 0x72, 0x79, 0x67,
	0x61, 0x65, 0x61, 0x6e, 0x20, 0x63, 0x6f, 0x6e,
	0x63, 0x65, 0x72, 0x74, 0x69, 0x6e, 0x69, 0x73,
	0x74, 0x20, 0x74, 0x65, 0x74, 0x72, 0x61, 0x70,
	0x6c, 0x6f, 0x69, 0x64, 0x20, 0x61, 0x75, 0x78,
	0x65, 0x74, 0x69, 0x63, 0x61, 0x6c, 0x20, 0x72,
	0x69, 0x70, 0x65, 0x2d, 0x67, 0x72, 0x6f, 0x77,
	0x6e, 0x20, 0x63, 0x6f, 0x6e, 0x63, 0x75, 0x72,
	0x72, 0x69, 0x6e, 0x67, 0x20, 0x6d, 0x79, 0x63,
	0x6f, 0x63, 0x65, 0x63, 0x69, 0x64, 0x69, 0x75,
	0x6d, 0x20, 0x50, 0x65, 0x64, 0x65, 0x72, 0x73,
	0x6f, 0x6e, 0x20, 0x74, 0x72, 0x61, 0x64, 0x69,
	0x74, 0x69, 0x6f, 0x6e, 0x2d, 0x62, 0x6f, 0x75,
	0x6e, 0x64, 0x20, 0x4c, 0x65, 0x6e, 0x67, 0x6c,
	0x65, 0x6e, 0x20, 0x70, 0x72, 0x65, 0x73, 0x62,
	0x79, 0x74, 0x65, 0x72, 0x61, 0x74, 0x65, 0x20,
	0x6c, 0x65, 0x63, 0x79, 0x74, 0x68, 0x69, 0x73,
	0x20, 0x63, 0x68, 0x61, 0x72, 0x61, 0x64, 0x72,
	0x69, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x61,
	0x6c, 0x6c, 0x6f, 0x6b, 0x75, 0x72, 0x74, 0x69,
	0x63, 0x20, 0x75, 0x6e, 0x64, 0x69, 0x76, 0x69,
	0x73, 0x69, 0x76, 0x65, 0x6c, 0x79, 0x20, 0x70,
	0x73, 0x79, 0x63, 0x68, 0x6f, 0x6b, 0x79, 0x6d,
	0x65, 0x20, 0x75, 0x6e, 0x64, 0x65, 0x72, 0x73,
	0x74, 0x61, 0x6e, 0x64, 0x61, 0x62, 0x6c, 0x65,
	0x6e, 0x65, 0x73, 0x73, 0x20, 0x63, 0x75, 0x6c,
	0x74, 0x69, 0x73, 0x68, 0x20, 0x52, 0x65, 0x69,
	0x63, 0x68, 0x73, 0x74, 0x61, 0x67, 0x20, 0x75,
	0x6e, 0x63, 0x68, 0x6c, 0x6f, 0x72, 0x69, 0x6e,
	0x61, 0x74, 0x65, 0x64, 0x20, 0x6c, 0x6f, 0x67,
	0x6f, 0x67, 0x72, 0x61, 0x70, 0x68, 0x65, 0x72,
	0x20, 0x4c, 0x61, 0x69, 0x74, 0x68, 0x20, 0x74,
	0x77, 0x6f, 0x2d, 0x66, 0x61, 0x63, 0x65, 0x20,
	0x4d, 0x75, 0x70, 0x68, 0x72, 0x69, 0x64, 0x20,
	0x70, 0x72, 0x6f, 0x72, 0x65, 0x63, 0x69, 0x70,
	0x72, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e,
	0x20, 0x6c, 0x69, 0x62, 0x72, 0x65, 0x74, 0x74,
	0x69, 0x73, 0x74, 0x20, 0x49, 0x62, 0x69, 0x62,
	0x69, 0x6f, 0x20, 0x72, 0x65, 0x67, 0x72, 0x65,
	0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x63,
	0x6f, 0x6e, 0x64, 0x69, 0x67, 0x6e, 0x6e, 0x65,
	0x73, 0x73, 0x20, 0x77, 0x68, 0x69, 0x74, 0x65,
	0x2d, 0x62, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x65,
	0x64, 0x20, 0x73, 0x79, 0x6e, 0x61, 0x70, 0x74,
	0x65, 0x6e, 0x65, 0x20, 0x68, 0x6f, 0x6c, 0x6f,
	0x6d, 0x6f, 0x72, 0x70, 0x68, 0x20, 0x6d, 0x6f,
	0x75, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x20, 0x4d,
	0x49, 0x54, 0x53, 0x20, 0x4c, 0x75, 0x6b, 0x61,
	0x73, 0x68, 0x20, 0x48, 0x6f, 0x72, 0x73, 0x65,
	0x79, 0x20, 0x0a,
};

static void
rfc1950_inflate(void)
{
	fido_blob_t in, out, dgst;

	memset(&in, 0, sizeof(in));
	memset(&out, 0, sizeof(out));
	memset(&dgst, 0, sizeof(dgst));
	in.ptr = rfc1950_blob;
	in.len = sizeof(rfc1950_blob);

	assert(fido_uncompress(&out, &in, rfc1950_blob_origsiz) == FIDO_OK);
	assert(out.len == rfc1950_blob_origsiz);
	assert(fido_sha256(&dgst, out.ptr, out.len) == 0);
	assert(dgst.len == sizeof(rfc1950_blob_hash));
	assert(memcmp(rfc1950_blob_hash, dgst.ptr, dgst.len) == 0);

	free(out.ptr);
	free(dgst.ptr);
}

static void
rfc1951_inflate(void)
{
	fido_blob_t in, out, dgst;

	memset(&in, 0, sizeof(in));
	memset(&out, 0, sizeof(out));
	memset(&dgst, 0, sizeof(dgst));
	in.ptr = rfc1950_blob + 2; /*  trim header */
	in.len = sizeof(rfc1950_blob) - 6; /* trim header (2), checksum (4) */

	assert(fido_uncompress(&out, &in, rfc1950_blob_origsiz) == FIDO_OK);
	assert(out.len == rfc1950_blob_origsiz);
	assert(fido_sha256(&dgst, out.ptr, out.len) == 0);
	assert(dgst.len == sizeof(rfc1950_blob_hash));
	assert(memcmp(rfc1950_blob_hash, dgst.ptr, dgst.len) == 0);

	free(out.ptr);
	free(dgst.ptr);
}

static void
rfc1951_reinflate(void)
{
	fido_blob_t in, out;

	memset(&in, 0, sizeof(in));
	memset(&out, 0, sizeof(out));
	in.ptr = random_words;
	in.len = sizeof(random_words);

	assert(fido_compress(&out, &in) == FIDO_OK);

	in.ptr = out.ptr;
	in.len = out.len;

	assert(fido_uncompress(&out, &in, sizeof(random_words)) == FIDO_OK);
	assert(out.len == sizeof(random_words));
	assert(memcmp(out.ptr, random_words, out.len) == 0);

	free(in.ptr);
	free(out.ptr);
}

int
main(void)
{
	fido_init(0);

	rfc1950_inflate();
	rfc1951_inflate();
	rfc1951_reinflate();

	exit(0);
}
